﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Speech.Recognition;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

namespace DigiMate
{
	public class WeatherService : BaseService
	{
		public WeatherService()
		{
		}

		public override void Run(string text)
		{
			if (inProcess)
				return;

			inProcess = true;
			try
			{
				string url = System.Configuration.ConfigurationManager.AppSettings["RssURL.Weather"];
				XmlReader reader = XmlReader.Create(url);
				SyndicationFeed feed = SyndicationFeed.Load(reader);
				reader.Close();

				bool firstTime = true;
				if (feed.Items.Count() > 0)
				{
					foreach (SyndicationItem item in feed.Items)
					{
						if (firstTime)
						{
						}

						
						String subject = item.Title == null ? "" : item.Title.Text;
						String summary = item.Summary == null ? "no details" : item.Summary.Text;
						DateTime date = item.PublishDate.DateTime;

						if (subject == "")
							continue;

						Speaker.SpeakOk(subject);
						Speaker.Speak(summary.Length > cutLongSummaryTo ? summary.Substring(0, cutLongSummaryTo) : summary);
					
					}
				}
				else
				{
					AddLog("No new messages in RSS");
				}
			}
			catch (Exception ex)
			{
				AddLog("There was a problem with rss: " + ex.Message);
			}
			finally
			{
				AddLog("Weather Stopped.");

				inProcess = false;
			}
		}
	}
}
